package admin

import (
	"net/http"

	"gitee.com/cxssang/goframe/lib/sql"

	. "gitee.com/cxssang/genealogy/act"
)

func init() {
	HandleFunMap["/admin/index"] = func(w http.ResponseWriter, r *http.Request) {
		sq := sql.New()
		parent, err := sq.Select("members", sql.NewWhere().Add("parent_id=0"), 0, 0, "name", "").FetchOne()
		if err != nil {
			DisplayError(w, err.Error())
			return
		}
		if parent == nil {
			parent = &sql.Row{}
			parent.Set("name", "未设置")
		}
		_settings, err := sq.Select("setting", nil, 0, 0, "key,value", "").FetchAll()
		if err != nil {
			DisplayError(w, err.Error())
			return
		}
		var settings = map[string]string{}
		for _, v := range _settings {
			settings[v.Get("key")] = v.Get("value")
		}
		if _, ok := settings["name"]; !ok {
			settings["name"] = "未设置"
		}
		count, err := sq.Count("members", nil, "id")
		Display(w, r, "admin/index", map[string]interface{}{
			"parent":   parent,
			"count":    count,
			"settings": settings,
		})
	}
}
